#include "dialogformat.h"
#include "ui_dialogformat.h"

DialogFormat::DialogFormat(QMap<CFormat, QString> &map, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::DialogFormat)
{
    m_ui->setupUi(this);
    this->map = &map;
    QMapIterator<CFormat, QString> i(*this->map);
    while (i.hasNext()) {
        i.next();
        QList<QListWidgetItem*> items = m_ui->listWidgetPaper->findItems(i.key().paperSize, Qt::MatchExactly);
        if (items.length() == 0)
            m_ui->listWidgetPaper->addItem(i.key().paperSize);
    }

    connect(m_ui->pushButtonOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect(m_ui->listWidgetPaper, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(UpdateCut()));
    connect(m_ui->listWidgetCut, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(UpdatePrSh()));
}

DialogFormat::~DialogFormat()
{
    delete m_ui;
}

QString DialogFormat::GetPressSheet()
{
    return m_ui->lineEditPrSh->text();
}

void DialogFormat::UpdateCut()
{
    m_ui->listWidgetCut->clear();
    QString paper_size = m_ui->listWidgetPaper->currentItem()->text();
    QMapIterator<CFormat, QString> i(*this->map);
    while (i.hasNext()) {
        i.next();
        if (i.key().paperSize == paper_size)
            m_ui->listWidgetCut->addItem(i.key().cut);
    }
}

void DialogFormat::UpdatePrSh()
{
    QString paper_size = m_ui->listWidgetPaper->currentItem()->text();
    QString paper_cut = m_ui->listWidgetCut->currentItem()->text();
    QString pr_sh = map->value(CFormat(paper_size, paper_cut));
    m_ui->lineEditPrSh->setText(pr_sh);
}

void DialogFormat::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
